<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Memberlist extends CI_Controller {

    public function index($page = 0) {
        
    	$sesMember = $this->session->userdata('sessionMember');
    	
    	if (!$sesMember) redirect(base_url() . 'memberlogin');

        $this->load->model("register_model");
        
        $check = $this->register_model->checkMember($sesMember);

        switch ($check['status']) {
            case "0":
                redirect(base_url() . 'memberlogin');
                break;
            case "1":
                redirect(base_url() . 'member/profile');
                break;
            case "2":
            	$this->_list($page);
                break;
        }
    	
    }

    private function _list($page) {
    	
    	$sesMember	= $this->session->userdata('sessionMember');
    	$data		= array ();
    	
		$this->load->model('tree_model');

		$data['member']		= $this->tree_model->getNode($sesMember);
		$data['child']		= $this->tree_model->countChild($data['member']);
    	
    	$data['member']['limit']	= 10;
    	$data['member']['offset']	= (int) $page;  
    	
    	$data['list']		= $this->tree_model->getChild($data['member']);
    	
    	$this->load->library('pagination');
    	
    	$config['base_url']		= site_url('memberlist/index/');
		$config['total_rows']	= $data['child'];
		$config['per_page']		= $data['member']['limit'];
		$config['uri_segment']	= 3;
		
		$this->pagination->initialize ( $config );
		
		$data ['paging'] = $this->pagination->create_links ();
    	
		$data ['template'] ['nav'] = "essential/member_header";
		$data ['template'] ['content'] = 'member/memberlist';
		$data ['template'] ['nosidebar'] = TRUE;
		
		$this->load->view ( 'essential/template', $data );
    }
}